import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

##
##print "___________________Car DB_____________________________________"
##print "______________________________________________________________"

           

class Car(db.Model):
        company = db.StringProperty(required=True)
        model = db.StringProperty(required=True)
        year = db.IntegerProperty()
        isManual = db.BooleanProperty()
        h_mpg = db.IntegerProperty()
        l_mpg = db.IntegerProperty()

        def printCar(self):
                print "The car properties: Company: " + self.company + " Model: " + self.model + " Year: " + str(self.year) + "Is Manual: " + str(self.isManual) + " High MPG: " + str(self.h_mpg) + " Low MPG: " + str(self.l_mpg)
                
        def getCarData(self):
                return "The car properties: Company: " + self.company + " Model: " + self.model + " Year: " + str(self.year) + " High MPG: " + str(self.h_mpg) + " Low MPG: " + str(self.l_mpg)

        def getMPGData(self):
                return "The MPG Data:  Highway MPG:" + str(self.h_mpg) + "   Urban MPG: " + str(self.l_mpg)
        
  
  
F=open('cars.csv','r')
data=F.readlines()
for line in data:        
        columns=line.split(",")      
        new_car = Car(company = columns[0],
                      model = columns[1],
                      year = int(columns[2]),
                      isManual = bool(int(columns[3])),
                      h_mpg = int(columns[4]),
                      l_mpg = int(columns[5]))
        new_car.put()
        
##        new_car.printCar()
##        print new_car.isManual
 
F.close()







class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write("""
      <html>
        <body>
        <h1> THE DRIVE COSTING SYSTEM </h1>
        <hr />
          <form action="/find" method="post">
          <p>
                Company name:
                <input type="text" name="company" />
                <br />
          </p>
          <p>
                Model name:
                <input type="text" name="model" />
                <br />
          </p>
          <p>
                Year (XXXX):
                <input type="text" name="year" />
                <br />
          </p>
          <p>

                <input type="radio" name="isManual" value="0" /> Automatic               
                <input type="radio" name="isManual" value="1" /> Manual

          </p>
          <hr />
          <p>

                Fuel Price (Per Liter) [nis]:
                <input type="text" name="price" value = "0"/>
                <br />

          </p>
          <hr />
          <p>
                Highway Length [km]:
                <input type="text" name="h_length" value = "0"/>
                <br />
           </p>
           <p>
                Urban Length [km]:
                <input type="text" name="l_length" value = "0"/>
                <br />
           </p>
          <hr />
                
            <div><input type="submit" value="Find The Drive Cost"></div>
          </form>
          <hr />
        </body>
      </html>""")


## Convert Mile per Galon to Liter per Kilometer
def ConvertMpgToLPK(mpg):
        ## 1 mile per gallon = 0.425143707 kilometers per liter
        return 1 / (mpg * 0.425143707)


def CalcDriveCost(price, h_mpg, l_mpg , h_lenght, l_lenght):
        return price * ((ConvertMpgToLPK(l_mpg) * l_lenght) + (ConvertMpgToLPK(h_mpg) * h_lenght))

        


class FindCar(webapp.RequestHandler):
  def post(self):          
                  f_car = Car.gql("WHERE company = :company AND model = :model AND year = :year AND isManual = :isManual",
                          company = self.request.get('company'),
                          model = self.request.get('model'),
                          year = int(self.request.get('year')),
                          isManual =  bool(int(self.request.get('isManual'))))

                  self.response.out.write('<html><body><h3>The result for:</h3><pre>')
                  self.response.out.write(cgi.escape(self.request.get('company')))
                  self.response.out.write('<br>')
                  self.response.out.write(cgi.escape(self.request.get('model')))
                  self.response.out.write('<br>')
                  self.response.out.write(cgi.escape(self.request.get('year')))
                  self.response.out.write('<br>')
                  self.response.out.write('Is Manual = ') 
                  self.response.out.write(cgi.escape(str(bool(int(self.request.get('isManual'))))))                 
                  self.response.out.write('<br>')

                  self.response.out.write('<br>')
                  self.response.out.write('Fuel Price [nis]: = ') 
                  self.response.out.write(cgi.escape(str(self.request.get('price'))))                 
                  self.response.out.write('<br>')

                  self.response.out.write('<br>')
                  self.response.out.write('Highway length = ') 
                  self.response.out.write(cgi.escape(str(self.request.get('h_length'))))
                  self.response.out.write('<br>')

                  self.response.out.write('<br>')
                  self.response.out.write('Highway length = ') 
                  self.response.out.write(cgi.escape(str(self.request.get('h_length'))))
                  self.response.out.write('<br>')
                  
                  self.response.out.write('<br>')
                  self.response.out.write('<hr>')
                  self.response.out.write('<br>')

                  try:
                          self.response.out.write(cgi.escape(f_car[0].getMPGData()))
                          self.response.out.write('<br>')
                          self.response.out.write('<br>')
                          self.response.out.write('The drive costing is:  ')
                          self.response.out.write(cgi.escape(str(CalcDriveCost(float(self.request.get('price')),
                                                                               f_car[0].h_mpg,
                                                                               f_car[0].l_mpg,                                                                           
                                                                               float(self.request.get('h_length')),
                                                                               float(self.request.get('l_length'))
                                                                               ))))
                          self.response.out.write('  nis')
                          self.response.out.write('<br>')
                          
                          
                  except:
                          self.response.out.write("Can`t find the car!")
                          self.response.out.write('</pre></body></html>')

        
           
                  

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/find', FindCar)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()







